package Exception;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        GirlFriend gf = new GirlFriend();
        while (true) {
            try {
                System.out.println("请输入女朋友的名字");
                String name = sc.next();
                gf.setName(name);
                System.out.println("请输入女朋友的年龄");
                String ageStr = sc.next();
                int age = Integer.parseInt(ageStr);
                gf.setAge(age);
                break;
            } catch (NumberFormatException e) {
                e.printStackTrace();

            }catch (NameFormatException e) {
                e.printStackTrace();
            }catch (AgeOutOfBoundsException e){
                e.printStackTrace();
            }
        }
        System.out.println(gf);
    }
}
class GirlFriend{
    private String name;
    private int age;

    public GirlFriend() {
    }

    public GirlFriend(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.length() < 3 || name.length() > 10){
            throw new NameFormatException(name + "的格式有误，应在3-10位");
        }
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age < 18 || age > 40){
            throw new AgeOutOfBoundsException(age + "应在18-40岁之间");
        }
        this.age = age;
    }

    @Override
    public String toString() {
        return "GirlFriend{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class NameFormatException extends RuntimeException{
    public NameFormatException() {
    }


    public NameFormatException(String s) {
        super(s);
    }
}
class AgeOutOfBoundsException extends RuntimeException{
    public AgeOutOfBoundsException(String message) {
        super(message);
    }

    public AgeOutOfBoundsException() {
    }
}
